# app/models/user.rb
# 继承自 Sequel::Model，这样它可以自动与 users 表进行交互。(:my_posts)指定表名
class User < Sequel::Model(:fruit)
#   attr_accessor :id, :name
#   def initialize(id, name)
#      @id = id
#      @name = name
#   end
#   def to_json(*args)
#      {
#         id: @id,
#         name: @name
#      }.to_json(*args)
#   end

  # one_to_many :orders
  # 你可以选择将 email 字段设置为唯一值
  plugin :validation_helpers

  def validate
    super
    validates_presence [:name, :email]  # 验证 name 和 email 的必填
    validates_unique :email             # 验证 email 唯一
    validates_format /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i, :email, message: 'is not a valid email'
  end
# CREATE TABLE users (
#   id SERIAL PRIMARY KEY,
#   name VARCHAR(255) NOT NULL,
#   email VARCHAR(255) NOT NULL
# );

# CREATE INDEX index_users_on_email ON users(email);

end
